package com.wang.designpattern.mediator;

public class CoffeeMachine extends Colleague {

    public CoffeeMachine(Mediator mediator, String name) {
        super(mediator, name);
        // 在创建Alarm同事对象时，将自己放入ConcreteMediator对象里的集合中
        mediator.register(name, this);
    }

    public void startCoffee() {
        System.out.println("startCoffee");
    }

    public void finishCoffee() {
        try {
            Thread.sleep(3000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("几分钟后，咖啡煮好了");
        System.out.println("finishCoffee");
        this.sendMessage(0);
    }

    @Override
    public void sendMessage(int stateChange) {
        // 调用中介者对象的getMessage方法
        this.getMediator().getMessage(stateChange, this.name);
    }

    @Override
    public void receiveMessage(int stateChange) {
        if (1 == stateChange) {
            finishCoffee();
        } else {
            startCoffee();
        }
    }
}
